package org.budo.dubbo.rpc.cluster.merger;

import com.alibaba.dubbo.rpc.cluster.Merger;

/**
 * 调用多个服务，返回数组，如果其中一个为true，就返回true
 * 
 * @author limingwei
 * 
 * @see com.alibaba.dubbo.rpc.cluster.merger.ArrayMerger
 */
public class BooleanAnyTrueArrayMerger implements Merger {
    @Override
    public Object merge(Object... others) {
        if (null == others || others.length == 0) {
            return null;
        }

        Boolean[] result = new Boolean[others.length];
        for (int i = 0; i < others.length; i++) {
            result[i] = this.anyTrue(i, (Boolean[][]) others);
        }
        return result;
    }

    private Boolean anyTrue(int i, Boolean[]... others) {
        for (Boolean[] each : others) {
            if (null != each[i] && each[i]) {
                return true;
            }
        }

        return false;
    }
}